package com.zrrd.controller;


import com.zrrd.common.vo.RulePageVo;
import com.zrrd.entity.Rule;
import com.zrrd.service.RuleService;
import com.zrrd.common.util.ResponseResult;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wanjl
 * @since 2024-09-09
 */
@RestController
@RequestMapping("/admin/rule")
@ResponseBody
public class RuleController {

    @Resource
    private RuleService ruleService;

    @ApiOperation(value = "分页查询角色列表",notes = "分析查询角色列表")
    @GetMapping("/{page}")
    @ResponseBody
    public ResponseResult findByPage(@PathVariable("page") Integer page){
        RulePageVo rolePageVo=ruleService.findByPage(page);
        return ResponseResult.queryResponse(rolePageVo);
    }

    /**
     * 新增
     * @param rule
     * @return
     */
    @PostMapping()
    public ResponseResult add(@RequestBody Rule rule){    //如果参数是某个实体类，那么注意在参数类型前加@RequestBody注解
        return ResponseResult.saveResponse(ruleService.save(rule));  //新增，把对象里的属性，新增到MySQL数据库
    }

    /**
     * 修改
     * @param rule
     * @return
     */
    @PutMapping
    public ResponseResult update(@RequestBody Rule rule){
        return ResponseResult.updateResponse(ruleService.updateById(rule)); //修改，根据id修改数据库的内容
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}/delete")
    public ResponseResult delete(@PathVariable("id") Integer id){ //如果参数类型不是实体类，而是其他如整数等类型，那么添加@PathVariable 注解
        return ResponseResult.deleteResponse(ruleService.removeById(id)); //删除，根据id删除数据库的数据
    }

    //修改状态
    @PutMapping("/{id}/updateStatus")
    public ResponseResult updateStatus(@PathVariable("id")Integer id,@RequestBody Rule rule){
        boolean flag = ruleService.lambdaUpdate()
                .eq(Rule::getId, id)
                .set(Rule::getStatus, rule.getStatus())
                .update();

        return ResponseResult.updateResponse(flag);
    }

}

